package components
{
	import UIFree.FX;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	import UIFree.uif_transition;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;

	public class pj_popup extends uif_ecomponent {
		private var _popupCtrl: uif_ecomponent;
		private var _timer: Timer;
		
		public function pj_popup(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			visible = false;
			alpha = 0;
			_timer = new Timer(500, 1);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, doTUnFocus);
			addEvent(MouseEvent.ROLL_OUT, doUnFocus);
		}
		
		public function eventsEcho(showEvent: String, hideEvent: String): void {
			addEvent(showEvent, doFocus, app);
			addEvent(hideEvent, doUnFocus, app);
		}
		
		public function get bottomZoom(): Number {
			return scaleX;
		} 
		
		public function set bottomZoom(a_zoom: Number): void {
			if (_popupCtrl != null) {
				var rect: Rectangle = _popupCtrl.getBounds(parent);
				var p: Point = new Point(rect.x + (rect.width - width) / 2, rect.y - height - arrowSize);
				var mat: Matrix = new Matrix();
				mat.translate(-width / 2, -height);
				mat.scale(a_zoom, a_zoom);
				mat.translate(p.x + width / 2, p.y + height);
				transform.matrix = mat;
			}
		} 
		
		protected function doFocus(e: Event): void {
			if (_timer.running) _timer.stop();
			popup(e.target as uif_ecomponent);
		}
		
		protected function doUnFocus(e: Event): void {
			_timer.start();
		}
		
		protected function doTUnFocus(e: Event): void {
			if (!this.hitTestPoint(app.mouseX, app.mouseY)) {
				slideHide();
				_popupCtrl = null;
			}
		}
				
		override protected function paint(a_styleClass: String): void {
			this.draw_hint(0, 0, width, height);
		}
		
		public function get popupCtrl(): uif_ecomponent {
			return _popupCtrl;
		}
		
		public function popup(hitCtrl: uif_ecomponent): void {
			_popupCtrl  = hitCtrl;
			var rect: Rectangle = hitCtrl.getBounds(parent);
			var p: Point = new Point(rect.x + (rect.width - width) / 2, rect.y - height - arrowSize);
			var afx: FX = getFX('alpha');
			if (afx) {
				this.FXNameCancel('alpha');
				visible = false;
			}
			
			if (!visible) {
				slideShow();
				fx('bottomZoom', (bottomZoom==1)?0.1:bottomZoom, 1, uif_transition.easeInOut(uif_transition.Back, 2)).start();
			} else {
				fx('y', y, rect.y - height - arrowSize).start();
				fx('x', x, rect.x + (rect.width - width) / 2).start();
			}
		}
		
		protected function get arrowSize(): Number {
			return height / 8;
		}
		
		protected function draw_hint(x: int, y: int, width: int, height: int): void {
			begin2(style);
			var rounds: Array = [0, 0, 0, 0];
			var hsize: int = arrowSize; 
			if (typeof(style.rounds) == 'number') {
				rounds[0] = style.rounds;
				rounds[1] = style.rounds;
				rounds[2] = style.rounds;
				rounds[3] = style.rounds;
			} else if (typeof(style.rounds) == 'object') rounds = style.rounds;
			
			this.graphics.moveTo(x + rounds[0], y);
			this.graphics.lineTo(x + width - rounds[1], y);
			this.graphics.curveTo(x + width, y, x + width, y + rounds[1]);
			this.graphics.lineTo(x + width, y + height - rounds[2]);
			this.graphics.curveTo(x + width, y + height, x + width - rounds[2], y + height);
			this.graphics.lineTo(x + width / 2 + hsize, y + height);
			this.graphics.lineTo(x + width / 2, y + hsize + height);
			this.graphics.lineTo(x + width / 2 - hsize, y + height);
			this.graphics.lineTo(x + rounds[3], y + height);
			this.graphics.curveTo(x, y + height, x, y + height - rounds[3]);
			this.graphics.lineTo(x, y + rounds[0]);
			this.graphics.curveTo(x, y, x + rounds[0], y);
			if (style.fill_color) this.graphics.endFill();
		}	
	}
}